package org.itboys.mobile.controller.web.business;

import com.wordnik.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.order.SundayOrder;
import org.itboys.mobile.entity.mysql.order.SundayOrderProduct;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mongo.web.business.SundayBusinessService;
import org.itboys.mobile.service.mysql.web.order.SundayOrderService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商户_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/business")
public class SundayBusinessController extends BaseController {

    @Autowired
    private SundayBusinessService businessService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayOrderService orderService;
    /**
     * 商户主页  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         HttpServletResponse response,
                           Model model){
        return "/sunday/business/index";

    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {
            PageResult<SundayMember> result=memberService.selectMember(request, SundayCommonConstants.user_type_business);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    /**
     * 新增或修改商户
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayMember business

    ) {
        try {
            businessService.saveBusiness(business,null);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 商户详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=false)Long id,
            Model model){

        SundayMember business = businessService.findOne(id);
        model.addAttribute("business", business);

        return "/sunday/business/input";
    }

    /**
     * 商户个人中心页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/input2")
    public String input2(
            HttpServletRequest request,
            Model model){

        SundayMember business = businessService.findOne(AdminSessionHolder.getPlatformMemberId());
        model.addAttribute("business", business);
        return "/sunday/business/input2";
    }
    /**
     * 冻结或解冻
     * @param request
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping("/change")
    public  void change(HttpServletRequest request,
                                HttpServletResponse response,
                                @RequestParam(value="id",required=false) Long id,
                                @RequestParam(value="status",required=false) Integer status

    ) {
        try {
            memberService.change(id, status);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/change------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 重置用户密码
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/resetPassword")
    public void resetPassword(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=true) Long id

    ) {
        try {
            businessService.resetPassword(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/resetPassword------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }
    /****************商品相关***************************/
    /**
     * 商品主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/product/index" )
    public  String product_index(HttpServletRequest request,
                         Model model){
        //判断是商户自己打开还是管理员打开
        if(StringUtils.isNotEmpty(request.getParameter("shopId"))){
            model.addAttribute("shopId",request.getParameter("shopId"));
        }else{
            model.addAttribute("shopId",AdminSessionHolder.getPlatformMemberId());
        }
        return "/sunday/business/product/index";
    }
    /**
     * 分页查询商品
     * @param request
     * @param response
     */
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                       HttpServletResponse response,
                        @RequestParam(value="shopId",required=true)Long shopId
    ) {
        try {

            PageResult<SundayProduct> result=productService.selectProduct(request,null,shopId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,CommonConstants.ERROMSG);
        }
    }

    /**
     * 商品详情页
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/product/input")
    public String product_input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="type",required=true)Integer type,
            @RequestParam(value="shopId",required=true)Long shopId,
            Model model){
        SundayProduct product = productService.findOne(id,type);
       // if(LongUtil.isNotZreo(shopId)){
            product.setShopId(shopId);
       // }
        model.addAttribute("product", product);
        return "/sunday/business/product/input";
    }



    /**
     * 新增或修改商品
     * @param request
     * @param response
     * @param product
     * @param imageFile
     * @param rollImageFiles
     */
    @RequestMapping("/product/save")
    public  void product_save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayProduct product,
                      @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                      @RequestParam(value="rollImageFiles",required=false) MultipartFile[] rollImageFiles
    ) {
        try {
            productService.save(product, imageFile,rollImageFiles);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/product/save------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 上下架
     * @param request
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping("/product/change")
    public  void product_change(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=false) Long id,
                        @RequestParam(value="status",required=false) Integer status

    ) {
        try {
            productService.change(id, status);
            ResponseMessage.success();

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/product/change------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**
     * 逐个删除某个质押物
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/product/delete")
    public void product_delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            productService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/product/delete------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**-----------------订单模块-------------------------------------*/
    /**
     * 订单模块主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/order/index")
    public String order_index(
            HttpServletRequest request,
            Model model
    ){
        //判断是商户自己打开还是管理员打开
        if(StringUtils.isNotEmpty(request.getParameter("shopId"))){
            model.addAttribute("shopId",request.getParameter("shopId"));
        }else{
            model.addAttribute("shopId",AdminSessionHolder.getPlatformMemberId());
        }
        return "/sunday/business/order/index";
    }

    /**
     * 订单详情
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/order/findOne")
    public String findOne(HttpServletRequest request,@RequestParam(value="id",required=true) Long id,Model model){
        SundayOrder order = orderService.findOne(id);
        model.addAttribute("order",order);
        return"/sunday/business/order/input";
    }

    /**
     *
     * @param request
     * @param response
     * @param orderStatus
     */
    @RequestMapping("/order/select")
    public void order_select(HttpServletRequest request,HttpServletResponse response,
                       @RequestParam(value="orderStatus",required=true) Integer orderStatus){
        try {
            PageResult<SundayOrder> result = orderService.selectOrder(request,orderStatus,null);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/order/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 分页获取订单商品
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/order/product/select")
    public void order_product_select(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="orderId",required=true) Long orderId
    ) {
        try {

            PageResult<SundayOrderProduct> result = orderService.selectOrderProduct(request,orderId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/order/product/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
    @RequestMapping("/order/send")
    public void order_send(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="orderId",required=true) Long orderId,
                     @RequestParam(value="sendCompany",required=false)String sendCompany,
                     @RequestParam(value="sendCompanyCode",required=false)String sendCompanyCode,
                     @RequestParam(value="sendCode",required=false) String sendCode
    ) {
        try {
            orderService.sendOrder(orderId,  sendCompany,sendCompanyCode,  sendCode);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/business/order/send------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }

}
